package com.daoImpl;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.Usin;
import com.dao.UsinDao;

/**
 * A data access object (DAO) providing persistence and search support for Usin
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.bean.Usin
 * @author MyEclipse Persistence Tools
 */
public class UsinDaoImpl extends HibernateDaoSupport implements UsinDao {
	private static final Logger log = LoggerFactory.getLogger(UsinDaoImpl.class);
	// property constants
	public static final String ID = "id";
	public static final String DGNO = "dgno";
	public static final String DPID = "dpid";
	public static final String USNA = "usna";
	public static final String PASS = "pass";
	public static final String IDCA = "idca";
	public static final String USTE = "uste";
	public static final String USSE = "usse";
	public static final String USEM = "usem";
	public static final String USCO = "usco";
	public static final String ORID = "orid";
	public static final String STAT = "stat";
	public static final String REMA = "rema";
	public static final String ISDH = "isdh";
	public static final String JOBN = "jobn";
	public static final String RENA = "rena";
	public static final String BRAD = "brad";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#save(com.bean.Usin)
	 */
	@Override
	public void save(Usin transientInstance) {
		log.debug("saving Usin instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#delete(com.bean.Usin)
	 */
	@Override
	public void delete(Usin persistentInstance) {
		log.debug("deleting Usin instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findById(java.lang.String)
	 */
	@Override
	public Usin findById(java.lang.String id) {
		log.debug("getting Usin instance with id: " + id);
		try {
			Usin instance = (Usin) getHibernateTemplate().get("com.bean.Usin",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByExample(com.bean.Usin)
	 */
	@Override
	public List findByExample(Usin instance) {
		log.debug("finding Usin instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Usin instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Usin as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findById(java.lang.Object)
	 */
	@Override
	public List findById(Object id) {
		return findByProperty(ID, id);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByDgno(java.lang.Object)
	 */
	@Override
	public List findByDgno(Object dgno) {
		return findByProperty(DGNO, dgno);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByDpid(java.lang.Object)
	 */
	@Override
	public List findByDpid(Object dpid) {
		return findByProperty(DPID, dpid);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByUsna(java.lang.Object)
	 */
	@Override
	public List findByUsna(Object usna) {
		return findByProperty(USNA, usna);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByPass(java.lang.Object)
	 */
	@Override
	public List findByPass(Object pass) {
		return findByProperty(PASS, pass);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByIdca(java.lang.Object)
	 */
	@Override
	public List findByIdca(Object idca) {
		return findByProperty(IDCA, idca);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByUste(java.lang.Object)
	 */
	@Override
	public List findByUste(Object uste) {
		return findByProperty(USTE, uste);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByUsse(java.lang.Object)
	 */
	@Override
	public List findByUsse(Object usse) {
		return findByProperty(USSE, usse);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByUsem(java.lang.Object)
	 */
	@Override
	public List findByUsem(Object usem) {
		return findByProperty(USEM, usem);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByUsco(java.lang.Object)
	 */
	@Override
	public List findByUsco(Object usco) {
		return findByProperty(USCO, usco);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByOrid(java.lang.Object)
	 */
	@Override
	public List findByOrid(Object orid) {
		return findByProperty(ORID, orid);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByStat(java.lang.Object)
	 */
	@Override
	public List findByStat(Object stat) {
		return findByProperty(STAT, stat);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByRema(java.lang.Object)
	 */
	@Override
	public List findByRema(Object rema) {
		return findByProperty(REMA, rema);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByIsdh(java.lang.Object)
	 */
	@Override
	public List findByIsdh(Object isdh) {
		return findByProperty(ISDH, isdh);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByJobn(java.lang.Object)
	 */
	@Override
	public List findByJobn(Object jobn) {
		return findByProperty(JOBN, jobn);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByRena(java.lang.Object)
	 */
	@Override
	public List findByRena(Object rena) {
		return findByProperty(RENA, rena);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findByBrad(java.lang.Object)
	 */
	@Override
	public List findByBrad(Object brad) {
		return findByProperty(BRAD, brad);
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Usin instances");
		try {
			String queryString = "from Usin";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.bean.UsinDao#merge(com.bean.Usin)
	 */
	@Override
	public Usin merge(Usin detachedInstance) {
		log.debug("merging Usin instance");
		try {
			Usin result = (Usin) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Usin instance) {
		log.debug("attaching dirty Usin instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Usin instance) {
		log.debug("attaching clean Usin instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static UsinDao getFromApplicationContext(ApplicationContext ctx) {
		return (UsinDao) ctx.getBean("usinDao");
	}

	@Override
	public boolean isBmmgn(String usid) {
		 
		Session session = getSession();  
		SQLQuery query=null;
		String sql = "select * from usin inner join usgr on usgr.usid = usin.usid " +
				"where usin.usid = :usid";
		query=session.createSQLQuery(sql);
		query.setString("usid", usid);
		List list = query.list();
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}
}